export const formatTime = (time) => {
    if (time === null || time === '') return;
    const date = new Date(time); //处理时间格式
    const now = new Date(); //当前时间
    const diff = now.getTime() - date.getTime();
    // 如果是今天
    if (date.getDate() === now.getDate() && date.getMonth() === now.getMonth() && date.getFullYear() === now.getFullYear()) {
        if (date.getMinutes() < 10) {
            return `${date.getHours()}:0${date.getMinutes()}`;
        }
        return `${date.getHours()}:${date.getMinutes()}`;
    }
    // 如果是昨天
    if (date.getDate() === now.getDate() - 1 && date.getMonth() === now.getMonth() && date.getFullYear() === now.getFullYear()) {
        return '昨天';
    }
    // 如果是前天
    if (date.getDate() === now.getDate() - 2 && date.getMonth() === now.getMonth() && date.getFullYear() === now.getFullYear()) {
        return '前天';
    }
    // 如果是一周内
    if (diff < 604800000) { // 一周的毫秒数
        const daysAgo = Math.floor(diff / 86400000);
        return `${daysAgo}天前`;
    }
    // 超过一周，返回月日格式
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的，所以+1
    const day = ("0" + date.getDate()).slice(-2);
    return `${year}/${month}/${day}`;
}